package com.slliver.sentinelservice.handler;


import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.event.SmartApplicationListener;

import java.util.List;

/**
 * sentinel规则持久化到nacos处理器
 */
public class SentinelNacosDataSourceHandler implements SmartInitializingSingleton {

    private final SentinelProperties sentinelProperties;

    public SentinelNacosDataSourceHandler(SentinelProperties sentinelProperties){
        this.sentinelProperties = sentinelProperties;
    }

    @Override
    public void afterSingletonsInstantiated() {
        sentinelProperties.getDatasource().values().forEach(this::registryWriter);
    }

    private void registryWriter(DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration){
        final NacosDataSourceProperties nacosDataSourceProperties = dataSourcePropertiesConfiguration.getNacos();
        if(nacosDataSourceProperties == null){
            return;
        }

        final RuleType ruleType = nacosDataSourceProperties.getRuleType();
        // 通过数据源配置的ruleType来注册数据源
        switch (ruleType){
            case FLOW:
                //WritableDataSource<List<RuleType>> flowWrite = new NacosWritableDataSource<>();

                break;
            case DEGRADE:
                break;
            case PARAM_FLOW:
                break;
            case SYSTEM:
                break;
        }
    }


}
